#include "lrcpage.h"
#include "ui_lrcpage.h"
#include<QFile>
#include<QDebug>

LrcPage::LrcPage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::LrcPage)
{
    ui->setupUi(this);
    setWindowFlag(Qt::FramelessWindowHint);
    ui->hideBtn->setIcon(QIcon(":/images/xiala.png"));
   // connect(ui->hideBtn,&QPushButton::clicked,this,[=](){hide();});


    Animation = new QPropertyAnimation(this, "geometry", this);
    Animation->setDuration(250);
    Animation->setStartValue(QRect(9, 10, width(), height()));
    Animation->setEndValue(QRect(9, 10+height(), width(), height()));
    connect(ui->hideBtn, &QPushButton::clicked, this, [=]{Animation->start();});
    connect(Animation, &QPropertyAnimation::finished, this, [=]{hide();});
}

LrcPage::~LrcPage()
{
    delete ui;
}

bool LrcPage::parseLrc(const QString &lrcPath)
{
    lrcLine.clear();

    QFile file(lrcPath);
    if(!file.open(QFile::ReadOnly))
    {

        qDebug()<<"打开文件: "<<lrcPath;
        return false;
    }

    //读取
    while(!file.atEnd())
    {

        QString lrcWord = file.readLine(1024);
        // [00:17.94]那些失眠的⼈啊 你们还好吗
        // [0:58.600.00]你像⼀只⻜来⻜去的蝴蝶
        int left = lrcWord.indexOf('[');
        int right = lrcWord.indexOf(']');
        // 解析时间
        qint64 lineTime = 0;
        int start = 0;
        int end = 0;
        QString time = lrcWord.mid(left, right-left+1);
        // 解析分钟
        start = 1;
        end = time.indexOf(':');
        lineTime += lrcWord.mid(start, end-start).toInt()*60*1000;
        // 解析秒
        start = end + 1;
        end = time.indexOf('.', start);
        lineTime += lrcWord.mid(start, end-start).toInt()*1000;
        // 解析毫秒
        start = end+1;
        end = time.indexOf('.', start);
        lineTime += lrcWord.mid(start, end-start).toInt();
        // 解析歌词
        QString word = lrcWord.mid(right+1).trimmed();
        lrcLine.push_back( LyricLine(lineTime, word.trimmed()));



    }

    for(auto word:lrcLine)
    {

        qDebug()<<"text :"<<word.text<<"time  :"<<word.time;
    }

    return true;
}

int LrcPage::getLineLrcWordIndex(qint64 pos)
{
    if(lrcLine.isEmpty())
    {
    return -1;
    }

    if(pos<lrcLine[0].time)
        return 0;

    for(int i=1;i<lrcLine.size();i++)
    {


        if(pos>lrcLine[i-1].time&&pos<=lrcLine[i].time)
        {
            return i-1;
        }
    }

    return lrcLine.size()-1;

}

QString LrcPage::getLineLrcWord(qint64 index)
{
    if(index<0||index>=lrcLine.size())
    {
        return "";
    }
    return lrcLine[index].text;
}

void LrcPage::showLineLrcWord(int time)
{
    int index = getLineLrcWordIndex(time);
    qDebug()<<"index  :"<<index;
    if(-1 == index)
    {
    ui->line1->setText("");
    ui->line2->setText("");
    ui->line3->setText("");
    ui->lineCenter->setText("当前歌曲⽆歌词");
    ui->line4->setText("");
    ui->line5->setText("");
    ui->line6->setText("");
    }else
    {
    ui->line1->setText(getLineLrcWord(index-3));
    ui->line2->setText(getLineLrcWord(index-2));
    ui->line3->setText(getLineLrcWord(index-1));
    ui->lineCenter->setText(getLineLrcWord(index));
    ui->line4->setText(getLineLrcWord(index+1));
    ui->line5->setText(getLineLrcWord(index+2));
    ui->line6->setText(getLineLrcWord(index+3));
    }
}
